package com.rfsp.mcp.controller.hos;

import com.rfsp.common.log.TrcMsg;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.XssUtils;
import com.rfsp.mcp.pojo.po.OrgYbjPO;
import com.rfsp.common.pojo.po.UserPO;
import com.rfsp.mcp.pojo.vo.MiAccountVO;
import com.rfsp.mcp.pojo.vo.MiPayLogVO;
import com.rfsp.mcp.pojo.vo.PfmVO;
import com.rfsp.mcp.service.AccountService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.rfsp.common.base.ResultListVO;
import com.rfsp.mcp.pojo.sdto.MiPaySDTO;
import com.rfsp.common.pojo.sdto.PageSDTO;
import com.rfsp.mcp.pojo.vo.MiPayVO;
import com.rfsp.mcp.service.PayService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RequiresRoles(value={"mcp_hos"}, logical= Logical.AND)
@Controller("HosPayController")
@RequestMapping("/hos/pay")
public class HosPayController extends HosController {
    public Logger logger = LoggerFactory.getLogger(HosPayController.class);

	@Autowired
	PayService service;

    @Autowired
    private AccountService accountService;

    @RequestMapping("/")
    public String index1(ModelMap model, PageSDTO pageSDTO, MiPaySDTO sdto) {
        PfmVO pfm = ShiroUtils.getAttribute("mcp_pfm");
        String itf_type = pfm.getItfType().trim();  //0：宿迁；1：抚顺；2江浙；9其他
        if ("0".equals(itf_type)) {
            return index(model,pageSDTO,sdto);
        }else if ("1".equals(itf_type)) {
            return index_fs(model,pageSDTO,sdto);
        }else {
            return "/error/404";
        }
    }
    @RequestMapping("/index")
    public String index(ModelMap model, PageSDTO pageSDTO, MiPaySDTO sdto) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "HosPayController.index"));
        sdto=XssUtils.replaceXss(sdto);

        if(sdto.getPayNyBegin()!=null && !"".equals(sdto.getPayNyBegin())) {
            String ny = sdto.getPayNyBegin();
            ny = ny.replace("-", "");
            ny = ny.substring(0, 6);
            sdto.setPayNyNumBegin(Integer.valueOf(ny));
        }
        if(sdto.getPayNyEnd()!=null && !"".equals(sdto.getPayNyEnd())) {
            String ny = sdto.getPayNyEnd();
            ny = ny.replace("-", "");
            ny = ny.substring(0, 6);
            sdto.setPayNyNumEnd(Integer.valueOf(ny));
        }
        PfmVO pfm = ShiroUtils.getAttribute("mcp_pfm");
        UserPO currentUser = ShiroUtils.getCurrentUser();
        sdto.setTargetOrgId(currentUser.getOrgId());
        sdto.setQueryOrgId(currentUser.getOrgId());
        sdto.setPfmId(pfm.getId());
        PageInfo<MiPayVO> pageInfo = service.get(sdto, pageSDTO);
        service.combo(pageInfo.getList());
        sdto=XssUtils.replaceUnXss(sdto);
        ResultListVO<MiPayVO> vo = ResultListVO.success(pageInfo,sdto);
        model.put("vo", vo);

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "HosPayController.index"));
        return "hos/pay/index";
    }

    @RequestMapping("/index_fs")
    public String index_fs(ModelMap model, PageSDTO pageSDTO, MiPaySDTO sdto) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "HosPayController.index_fs"));
        sdto=XssUtils.replaceXss(sdto);
        //医疗机构代码
        String orgNo = "";
        //机构ID
        UserPO currentUser = ShiroUtils.getCurrentUser();
        Long orgId = currentUser.getOrgId();
        PfmVO pfm = ShiroUtils.getAttribute("mcp_pfm");
        OrgYbjPO ybjPO = ShiroUtils.getAttribute("org_ybj");
        if (ybjPO != null) {
            orgNo = ybjPO.getOrgNo();
        }
        //获取医保账户信息
        MiAccountVO accountVO = accountService.findByPfmIdAndOrgNo(pfm.getId(), orgNo, orgId);
        if (accountVO == null) {
            accountVO = new MiAccountVO();
        }
        sdto.setPfmId(pfm.getId());
        sdto.setOrgNo(orgNo);
        sdto.setQueryOrgId(orgId);
        sdto.setTargetOrgId(orgId);
        if (StringUtils.isNotBlank(sdto.getPayStartDate()) && StringUtils.isNotBlank(sdto.getPayEndDate())) {
            sdto.setPayDateFlag("1");
        } else if (StringUtils.isNotBlank(sdto.getPayStartDate()) && StringUtils.isBlank(sdto.getPayEndDate())) {
            sdto.setPayDateFlag("2");
        } else if (StringUtils.isBlank(sdto.getPayStartDate()) && StringUtils.isNotBlank(sdto.getPayEndDate())) {
            sdto.setPayDateFlag("3");
        }
        PageInfo<MiPayVO> pageInfo = service.getFs(sdto, pageSDTO);
        service.combo(pageInfo.getList());

        sdto=XssUtils.replaceUnXss(sdto);
        ResultListVO<MiPayVO> vo = ResultListVO.success(pageInfo,sdto);
        model.put("vo", vo);
        model.put("accountVo", XssUtils.replaceUnXss(accountVO));

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "HosPayController.index_fs"));
        return "hos/pay/index_fs";
    }
    /*
    @GetMapping("/detail")
    public String detail(ModelMap model,Long id) {
        ResultVO<MiPayVO> vo = ResultVO.success(new MiPayVO());
        model.put("vo", vo);

        return "hos/pay1/detail";
    }*/


    @GetMapping("/download")
    public void download(HttpServletResponse response) throws Exception {
        PfmVO pfm = ShiroUtils.getAttribute("mcp_pfm");
        String itf_type = pfm.getItfType().trim();  //0：宿迁；1：抚顺；2江浙；9其他
        if ("1".equals(itf_type)) {
            //医疗机构代码
            String orgNo = "";
            //机构ID
            UserPO currentUser = ShiroUtils.getCurrentUser();
            Long orgId = currentUser.getOrgId();
            OrgYbjPO ybjPO = ShiroUtils.getAttribute("org_ybj");
            if (ybjPO != null) {
                orgNo = ybjPO.getOrgNo();
            }
            //获取医保账户信息
            MiAccountVO accountVO = accountService.findByPfmIdAndOrgNo(pfm.getId(), orgNo, orgId);
            if (accountVO == null) {
                accountVO = new MiAccountVO();
            }
            MiPaySDTO sdto = new MiPaySDTO();
            sdto.setPfmId(pfm.getId());
            sdto.setOrgNo(orgNo);
            sdto.setQueryOrgId(orgId);
            sdto.setTargetOrgId(orgId);
            //获取结算信息
            List<MiPayVO> voList = service.getFs(sdto);
            List<MiPayVO> collect = voList.stream().map(vo -> XssUtils.replaceUnXss(vo)).collect(Collectors.toList());
            MiPayLogVO payLogVO = service.findPayLogByQueryOrgIdAndTargetOrgIdAndPfmId(sdto);
            String fileName = "医保结算信息表-" + System.currentTimeMillis() + ".xlsx";
            boolean deleteFile = true;
            service.buildDetailFile(XssUtils.replaceUnXss(accountVO), collect, fileName, payLogVO.getQueryTime());
            service.responseDetailFile(response, fileName, deleteFile);
        }
    }

}